<?php
class product_admin extends MX_Controller{
	public $category;
	public function index(){
		$limit=25;
		$paged=$this->input->post('paged')?$this->input->post('paged'):1;
		$filter=array('category'=>0,'date'=>'');
		$option=array();
		$paging=array('page_current'=>$paged-1,'number_affiche'=>$limit);
		if(isset($_POST)){
			$filter=$this->input->post('filter');
			$option=$this->input->post('option');
		}
		
		$adminMenu= $this->load->module('adminMenu/index');
		$category= $this->load->module('category/admin');
		$sort=array('prd_id'=>'desc','prd_counter'=>'desc','prd_name'=>'desc');
		if(isset($_POST['sort']))
			foreach ($sort as $key=>$val)
				if($this->input->post($key)) 
					$sort[$key]=$this->input->post($key);

		$setting =$this->load->Module('setting/admin');
		$this->template=$this->config->config['admin_template'];
		$word=$this->input->post('s');
		
		//prendre tous les id des produits
		$listAllIdProducts=$this->model()->get_list_product($filter,$option,$sort,$word,$paging);
		
		$listAllProducts=array();
		if(!empty($listAllIdProducts)){
			foreach ($listAllIdProducts as $idProduct){
				$product=$this->model()->get_info_product($idProduct->prd_id);
				$listAllProducts[$idProduct->prd_id]=$product;
			}
		}
		
		$option_filter_category=$category->wp_dropdown_cats($filter['category']);
		$option_filter_date=$this->model()->get_month_year_create();
		$tableCategoryHeader=array(0=>null,'name'=>'Name','slug'=>'url friendly','description'=>'Description','count'=>'Post');
		$category->listTableCategory();
		$setting= $this->load->module('setting/option');
		$category_option=$setting->getFilters($filter['category'],$option,true);
		
		$data=array(
					'update_footer'=>'StarView 2013',
					'admin_title'=>'Control Admin',
					'sort'=>$sort,
					'filter'=>$filter,
					'option_filter_category'=>$option_filter_category,
					'option_filter_date'=>$option_filter_date,
					'admin_menu'=>$adminMenu->generateAdminMenu(),
					'listAllProducts'=>$listAllProducts,
					'category_option'=>$category_option,
					'tableCategoryHeader'=>$tableCategoryHeader,
					'tableCategoryBody'=>$category->category,
					'totalActive'=>$this->model()->get_total_product(true),
					'limit'=>$limit,
					'paged'=>$paged,
					'total'=>$this->model()->get_total_product()
				);
		
		
		$this->view(
				array('admin-header',
						'product',
						'admin-footer'), 
				$data); // Display the page
	}

	public function new_product(){
		$adminMenu= $this->load->module('adminMenu/index');
		$category= $this->load->module('category/admin');
		
		$localisation= $this->load->module('localisation/index');
		$location=$localisation->getlocation();
		$regionList=$location->OptionRegion();
		$departementList=$location->OptionDepartement();
		$codePostal=$location->Postalcode;
		$villeList=$location->OptionVille($codePostal);
		


		$setting =$this->load->Module('setting/admin');
		$this->template=$this->config->config['admin_template'];
		
		$data=array(
				'update_footer'=>'StarView 2013',
				'admin_title'=>'Control Admin',
				'admin_menu'=>$adminMenu->generateAdminMenu(),
				'regionList'=>$regionList,
				'departementList'=>$departementList,
				'villeList'=>$villeList,
				'codePostal'=>$codePostal,
				'CategoryList'=>$category->dropdown_option_cats()
		);
		
		$this->view(
				array('admin-header',
						'product-new',
						'admin-footer'),
				$data); // Display the page
	}
	public function create(){
		$product=new stdclass;
		
		$product->prd_name= $this->input->post('prd_name');
		$product->prd_price=str_replace(',','.',$this->input->post('price'));
		$product->prd_description=$this->input->post('prd_description');
		$product->prd_tag=$this->input->post('tag');
		$product->prd_keywords=$this->input->post('keywords');
		$product->category_id=$this->input->post('category');
		$product->prd_link=$this->input->post('prd_link');
		$product->prd_codepostal=$this->input->post('cp');
		$product->prd_departement= $this->input->post('prd_departement');
		$product->prd_active=$this->input->post('prd_active');
		$product->date_create=strtotime('now');
		$product->date_validate=strtotime("{$this->input->post('aa')}-{$this->input->post('mm')}-{$this->input->post('jj')} {$this->input->post('hh')}:{$this->input->post('mm')}:00");
		$product->option=$this->input->post('option');
		$product->prd_youtube= $this->input->post('prd_youtube');
		
		$product->report= $this->input->post('report');
		$product->top=$this->input->post('top');
		$product->priority= $this->input->post('priority');
		$product->priority_date= $this->input->post('priority_date');
		$product->urgent=$this->input->post('urgent');
		$product->more_photo= $this->input->post('more_photo');
		$product->company= $this->input->post('company');
		$product->user_name= $this->input->post('user_name');
		$product->user_email= $this->input->post('user_email');
		$product->user_tel= $this->input->post('user_tel');
		$product->user_tel_show= $this->input->post('user_tel_show');
		$product->shop_id= $this->input->post('shop_id');
		$product->user_password= $this->input->post('user_password');
		$product->user_ip=$this->input->ip_address();
		
		$photos=$this->input->post('photo');
		//deplacer les photo dans bon dossier categories		
		$product->photo=self::copy_Photo(FCPATH."images/",$product->category_id,$photos);
		
		$id=$this->model()->create($product);
		
		
		//partie Indexation

		$indexation=$this->load->module("indexation/admin");
		
		$indexation->product_info_id=$id;
		//1 point pour description
		$indexation->chaine=$product->prd_description;
		$indexation->poids=1;
		$indexation->save();
		//2 point pour le tile
		$indexation->chaine=$product->prd_name;
		$indexation->poids=2;
		$indexation->save();

		//3 point pour le mots clé
		$indexation->chaine=$product->prd_keywords;
		$indexation->poids=3;
		$indexation->save();
		
		global $CFG;
		$url=$CFG->config['base_url'].'product/admin';
		$this->load->helper('url');
		redirect($url);
	}
	private function copy_Photo($path,$cat,$photos=array()){
		if(!empty($photos))
		foreach ($photos as $photo){
			$temp_photo=$path.$photo;
			$temp_thumb_photo=$path."thumbnails/".$photo;
			$NameCatPhoto=$cat."/$photo";
		
			if(!is_dir($path.$product->category_id))mkdir($path.$cat, 0777);
			if(!is_dir($path."thumbnails/".$cat))mkdir($path."thumbnails/".$cat, 0777);
		
			if (copy($temp_photo,"$path/$NameCatPhoto")) unlink($temp_photo);
			if (copy($temp_thumb_photo,$path."thumbnails/$NameCatPhoto")) unlink($temp_thumb_photo);
		
			$product_photo[]=$NameCatPhoto;
		}
		if(isset($product_photo)) return $product_photo;
		
		return false;
	}
	public function edit_product(){
		$product=new stdclass;
		$product->prd_id=$this->input->post('prd_id');
		$product->prd_name= $this->input->post('prd_name');
		$product->prd_price=str_replace(',','.',$this->input->post('price'));
		$product->prd_description=$this->input->post('prd_description');
		$product->prd_tag=$this->input->post('tag');
		$product->prd_keywords=$this->input->post('keywords');
		$product->category_id=$this->input->post('category');
		$product->prd_link=$this->input->post('prd_link');
		$product->prd_codepostal=$this->input->post('cp');
		$product->prd_active=$this->input->post('prd_active');
		$product->prd_youtube= $this->input->post('prd_youtube');
		
		$product->report= $this->input->post('report');
		$product->top=$this->input->post('top');
		$product->priority= $this->input->post('priority');
		$product->priority_date= $this->input->post('priority_date');
		$product->urgent=$this->input->post('urgent');
		$product->more_photo= $this->input->post('more_photo');
		$product->company= $this->input->post('company');
		$product->user_name= $this->input->post('user_name');
		$product->user_email= $this->input->post('user_email');
		$product->user_tel= $this->input->post('user_tel');
		$product->user_tel_show= $this->input->post('user_tel_show');
		$product->shop_id= $this->input->post('shop_id');
		$product->user_password= $this->input->post('user_password');
		
		$product->option=$this->input->post('option');
		$path=FCPATH."images/";
		$delPhoto=$this->input->post('delPhoto');
		if(!empty($delPhoto)){
			$this->model()->delPhoto(array_keys($delPhoto));
			foreach ($delPhoto as $photo){
				if(file_exists($path.$photo)) unlink($path.$photo);
				if(file_exists($path."thumbnails/".$photo))	unlink($path."thumbnails/".$photo);
			}
		}
		$photos=$this->input->post('photo');
		//deplacer les photo dans bon dossier de categories
		$product->photo=self::copy_Photo(FCPATH."images/",$product->category_id,$photos);
		if($product->prd_id)
			$this->model()->edit($product);
		global $CFG;
		$url=$CFG->config['base_url'].'product/admin';
		$this->load->helper('url');
		redirect($url);
	}
	public function deactive($id){
		$this->model()->deactive($id);
		$this->load->helper('url');
		redirect('product/admin');
	}
	public function reactive($id){
		$this->model()->reactive($id);
		$this->load->helper('url');
		redirect('product/admin');
	}
	public function delete($prd_id){
		//delette indexation
		$indexation=$this->load->module("indexation/admin");
		$indexation->delete($prd_id);
		//delette product
		$this->model()->delete($prd_id);
	}
	public function edit($prd_id){
		$adminMenu= $this->load->module('adminMenu/index');
		$category= $this->load->module('category/admin');
		$setting =$this->load->Module('setting/admin');
		$this->template=$this->config->config['admin_template'];
		
		$product= $this->model()->get_info_product($prd_id);
		$localisation= $this->load->module('localisation/index');
		$location=$localisation->getlocationCodePostal($product->prd_codepostal);
		$regionList=$location->OptionRegion();
		$departementList=$location->OptionDepartement();
		$codePostal=$location->Postalcode;
		$villeList=$location->OptionVille($codePostal);
		
		if(isset($product->option)){
			$option=$this->load->module('setting/option');
			$product->option=$option->getOptions($product->category_id,$product->option,true);
		}
		$data=array(
				'update_footer'=>'StarView 2013',
				'admin_title'=>'Control Admin',
				'admin_menu'=>$adminMenu->generateAdminMenu(),
				'regionList'=>$regionList,
				'departementList'=>$departementList,
				'villeList'=>$villeList,
				'codePostal'=>$codePostal,
				'CategoryList'=>$category->dropdown_option_cats($product->category_id),
				'product'=>$product
		);
		
		$this->view(
				array('admin-header',
						'product-edit',
						'admin-footer'),
				$data); // Display the page
	}
	
	
	private function checkURL(){
		if(isset($_POST) and isset($_POST['url'])){
			if($url=trim($_POST['url'])) echo $url;
		}
	}

}